import os
import requests
import pickle
from multiprocessing import Pool

def get_btc_block_by_height(height):
    file_height = 'E:/blocks/btc/by_height/' + str(height) + '.pkl'
    if os.path.exists(file_height):
        return
    if height % 10000 == 0:
        print(height)
    response = requests.get('https://blockchain.info/block-height/' + str(height) + '?format=json')
    if response.status_code == 200:
        block = response.json()['blocks'][0]

        with open(file_height, 'wb') as f:
            pickle.dump(block, f)

if __name__ == '__main__':
    pool = Pool(28)
    for i in range(886405, 886547):
        pool.apply_async(get_btc_block_by_height, (i,))
    pool.close()
    pool.join()
